<?php

namespace app\model;

use think\Model;

class SystemConfig extends Model
{
    // 表名
    protected $name = 'system_config';

    // 自动时间戳
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $createTime = 'created_at';
    protected $updateTime = 'updated_at';

    // 字段类型转换
    protected $type = [
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
        'is_public' => 'boolean',
    ];

    // 缓存配置
    private static $configCache = [];

    // 获取配置值
    public static function get($key, $default = null)
    {
        if (isset(self::$configCache[$key])) {
            return self::$configCache[$key];
        }

        $config = self::where('key', $key)->find();
        if (!$config) {
            return $default;
        }

        $value = $config->value;

        // 根据类型转换值
        switch ($config->type) {
            case 'int':
                $value = (int) $value;
                break;
            case 'bool':
                $value = (bool) $value;
                break;
            case 'json':
                $value = json_decode($value, true);
                break;
            default:
                // string 类型不需要转换
                break;
        }

        // 缓存结果
        self::$configCache[$key] = $value;

        return $value;
    }

    // 设置配置值
    public static function setConfig($key, $value, $description = null, $type = 'string', $isPublic = false)
    {
        // 根据类型转换值
        switch ($type) {
            case 'json':
                $value = json_encode($value);
                break;
            case 'bool':
                $value = $value ? '1' : '0';
                break;
            default:
                $value = (string) $value;
                break;
        }

        $config = self::where('key', $key)->find();
        if ($config) {
            $config->value = $value;
            if ($description !== null) {
                $config->description = $description;
            }
            $config->type = $type;
            $config->is_public = $isPublic;
            $result = $config->save();
        } else {
            $result = self::create([
                'key' => $key,
                'value' => $value,
                'type' => $type,
                'description' => $description,
                'is_public' => $isPublic,
            ]);
        }

        // 清除缓存
        unset(self::$configCache[$key]);

        return $result;
    }

    // 获取所有公开配置
    public static function getPublicConfigs()
    {
        $configs = self::where('is_public', 1)->select();
        $result = [];

        foreach ($configs as $config) {
            $value = $config->value;

            // 根据类型转换值
            switch ($config->type) {
                case 'int':
                    $value = (int) $value;
                    break;
                case 'bool':
                    $value = (bool) $value;
                    break;
                case 'json':
                    $value = json_decode($value, true);
                    break;
            }

            $result[$config->key] = $value;
        }

        return $result;
    }

    // 批量设置配置
    public static function setBatch($configs)
    {
        foreach ($configs as $key => $config) {
            $value = $config['value'] ?? '';
            $description = $config['description'] ?? null;
            $type = $config['type'] ?? 'string';
            $isPublic = $config['is_public'] ?? false;

            self::set($key, $value, $description, $type, $isPublic);
        }

        return true;
    }

    // 删除配置
    public static function remove($key)
    {
        unset(self::$configCache[$key]);
        return self::where('key', $key)->delete();
    }

    // 清除缓存
    public static function clearCache()
    {
        self::$configCache = [];
    }

    // 获取加密方式列表
    public static function getEncryptMethods()
    {
        return self::get('encrypt_methods', [
            'AES-256-GCM',
            'AES-256-CBC',
            'AES-128-GCM',
            '3DES',
            'SM4'
        ]);
    }

    // 检查配置键是否存在
    public static function keyExists($key)
    {
        return self::where('key', $key)->count() > 0;
    }
}